package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmUserLoginDto;
import com.heima.model.wemedia.entity.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.MD5Utils;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 自媒体用户表(WmUser)表服务实现类
 *
 * @author makejava
 * @since 2023-06-26 21:06:24
 */
@Service("wmUserService")
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService{
    @Resource
    private WmUserMapper wmUserMapper;
    /**
     * 新增自媒体用户
     * @param wmUser
     * @return
     */
    @Override
    public ResponseResult saveWmUser(WmUser wmUser) {

        //参数校验
        if(wmUser==null|| StringUtils.isEmpty(wmUser.getUsername())||StringUtils.isEmpty(wmUser.getPassword())||StringUtils.isEmpty(wmUser.getSalt())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        wmUserMapper.insert(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult login(WmUserLoginDto dto) {
        //1. 参数校验
        if (dto == null || StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2. 根据用户名查询用户信息
        LambdaQueryWrapper<WmUser> wrapper = Wrappers.<WmUser>lambdaQuery()
                .eq(WmUser::getUsername, dto.getName());
        //LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();
        //wrapper.eq(WmUser::getUsername,dto.getName());
        WmUser wmUser = wmUserMapper.selectOne(wrapper);
        if (wmUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "用户名错误!");
        }
        //3. 根据上一步返回用户信息中的盐 结合 用户输入的密码 进行加盐加密 得到密文
        String encode = MD5Utils.encodeWithSalt(dto.getPassword(), wmUser.getSalt());
        //4. 使用上一步得到的密文 和 数据库中保存的密文进行比对
        //4.1 比对失败, 密码错误 , 返回错误信息
        if (!StringUtils.equals(encode,wmUser.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //4.2 比对成功, 基于登录用户信息生成token
        String token = AppJwtUtil.getToken(wmUser.getId().longValue());
        //5. 封装数据返回
        Map<String,Object> result = new HashMap<>(2);
        wmUser.setPassword(null);
        wmUser.setSalt(null);
        result.put("user",wmUser);
        result.put("token",token);

        return ResponseResult.okResult(result);
    }


}

